package fr.jgraph.model.graph.old;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import fr.jgraph.model.edge.DirectedEdge;
import fr.jgraph.model.vertex.old.Vertex;

/** Graphe liste d'adjacence. */
public class GraphLA extends OrientedGraph {
	private ArrayList graphNodes;
	
	private GraphNode getNode(Vertex vertex) {
		Iterator itNodes = graphNodes.iterator();
		while(itNodes.hasNext()) {
			GraphNode node = (GraphNode) itNodes.next();
			if(node.vertex.equals(vertex))
				return node;
		}
		
		return null;
	}
	
	private GraphLA() {
		graphNodes = new ArrayList();
	}
	
	public GraphLA(Collection vertices, Collection edges) {
		this();
		
		Iterator itVertices = vertices.iterator();
		while(itVertices.hasNext()) {
			Vertex vertex = (Vertex) itVertices.next();
			graphNodes.add(new GraphNode(vertex));
		}
		
		Iterator itEdges = edges.iterator();
		while(itEdges.hasNext()) {
			DirectedEdge edge = (DirectedEdge) itEdges.next();
			getNode(edge.getSource()).addEdge(edge);
		}
	}
	
	/** Un noeud du graphe : 1 noeud + des arcs partant de ce noeud */
	class GraphNode {
		Vertex vertex;
		ArrayList edges;

		GraphNode(Vertex vertex) {
			this.vertex = vertex;
			edges = new ArrayList();
		}
		
		void addEdge(DirectedEdge edge){
			edges.add(edge.vertexSet());
		}
	}
	
	public Collection getVertices() {
		ArrayList list = new ArrayList();
		
		Iterator itNodes = graphNodes.iterator();
		while(itNodes.hasNext()) {
			GraphNode node = (GraphNode) itNodes.next();
			list.add(node.vertex);
		}
		
		return list;
	}

	public Collection getEdges() {
		ArrayList edges = new ArrayList();
		
		Iterator itNodes = graphNodes.iterator();
		while(itNodes.hasNext()) {
			GraphNode node = (GraphNode) itNodes.next();
			Iterator itEnds = node.edges.iterator();
			while(itEnds.hasNext()) {
				Vertex end = (Vertex) itEnds.next();
				edges.add(new DirectedEdge(node.vertex, end));
			}
		}
		
		return edges;
	}

	/* (non-Javadoc)
	 * @see fr.jgraph.model.graph.AbstractGraph#isComplete()
	 */
	public boolean isComplete() {
		Iterator itNodes = graphNodes.iterator();
		
		while(itNodes.hasNext()) {
			itNodes.next();
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see fr.jgraph.model.graph.AbstractGraph#areAdjacent(fr.jgraph.model.vertex.Vertex, fr.jgraph.model.vertex.Vertex)
	 */
	public boolean areAdjacent(Vertex a_v1, Vertex a_v2) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see fr.jgraph.model.graph.AbstractGraph#isVertexStable(java.util.Set)
	 */
	public boolean isVertexStable(Set a_set) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see fr.jgraph.model.graph.AbstractGraph#isIsomorphicTo(fr.jgraph.model.graph.AbstractGraph)
	 */
	public boolean isIsomorphicTo(AbstractGraph a_graph) {
		// TODO Auto-generated method stub
		return false;
	}

	/* (non-Javadoc)
	 * @see fr.jgraph.model.graph.AbstractGraph#isAutomorphicTo(fr.jgraph.model.graph.AbstractGraph)
	 */
	public boolean isAutomorphicTo(AbstractGraph a_graph) {
		// TODO Auto-generated method stub
		return false;
	}
}
